package com.cincc.cinccimdemo.ui.conversation.member;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.RemoveGroupMemberAdapter;
import com.cincc.cinccimdemo.adapter.RemovePickerUserAdapter;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.common.ContactUtil;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.cinccimdemo.model.GroupMemberInfoUI;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.viewmodel.GroupMemberListViewModel;
import com.cincc.cinccimdemo.widget.SideBarView;
import com.cincc.im.callback.ImCallback;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.ImResponse;

import java.util.ArrayList;
import java.util.List;

public class RemoveMemberActivity extends IMBaseActivity {
    @Override
    protected int contentLayout() {
        return R.layout.activity_add_member;
    }

    RecyclerView selectRecyclerView;
    RecyclerView recyclerView;

    SideBarView sideBarView;
    TextView mTextCenterContact;

    @Override
    protected void bindViews() {
        super.bindViews();
        recyclerView = findViewById(R.id.mList_contact);
        selectRecyclerView = findViewById(R.id.selectRecyclerView);
        sideBarView = findViewById(R.id.mSide_bar_contact);
        mTextCenterContact = findViewById(R.id.mText_center_contact);
    }

    @Override
    protected void bindEvents() {
        super.bindEvents();
    }

    RemoveGroupMemberAdapter groupMemberAdapter;

    RemovePickerUserAdapter pickerUserAdapter;
    GroupMemberListViewModel memberListViewModel;
    String groupId = "";

    @Override
    protected void afterViews() {
        super.afterViews();
        groupId = getIntent().getStringExtra(AttrConstants.GROUP_ID);
        groupMemberAdapter = new RemoveGroupMemberAdapter(this);
        recyclerView.setAdapter(groupMemberAdapter);

        memberListViewModel = new ViewModelProvider(this).get(GroupMemberListViewModel.class);
        memberListViewModel.memberListLiveData(groupId).observe(this, friendInfos -> {
            groupMemberAdapter.setContactList(friendInfos);
            setSideView();
        });

        groupMemberAdapter.setOnDataClickItem(new OnItemClickListener() {
            @Override
            public void onClick(int index) {
                groupMemberAdapter.updateItem(index);
                GroupMemberInfoUI item = groupMemberAdapter.getItem(index);

                if (item.isChecked) {
                    pickerUserAdapter.addItem(item);
                } else {
                    pickerUserAdapter.removeItem(item);
                }
                updateCount();
            }
        });

        pickerUserAdapter = new RemovePickerUserAdapter(this);
        selectRecyclerView.setAdapter(pickerUserAdapter);
        selectRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        pickerUserAdapter.setOnShowListener(count -> selectRecyclerView.setVisibility(count > 0 ? View.VISIBLE : View.GONE));
        pickerUserAdapter.setOnItemClickListener(index -> {
            GroupMemberInfoUI item = pickerUserAdapter.getItem(index);
            groupMemberAdapter.removeItem(item);
            pickerUserAdapter.removeItem(item);
            updateCount();
        });
    }

    private void updateCount() {
        int count = pickerUserAdapter.getItemCount();
        if (count == 0) {
            menuItem.setTitle("删除");
            menuItem.setEnabled(false);
        } else {
            menuItem.setTitle("删除(" + count + ")");
            menuItem.setEnabled(true);
        }
    }

    public void setSideView() {
        // 设置需要显示的索引栏内容
        String[] letters = ContactUtil.getLetters(groupMemberAdapter.getList());
        sideBarView.setLetter(letters);
        // 设置需要显示的提示框
        sideBarView.setTextView(mTextCenterContact);
        sideBarView.setOnTouchingLetterChangedListener(s -> {
            int position = ContactUtil.getLetterPosition(groupMemberAdapter.getList(), s);
            if (position != -1) {
                recyclerView.scrollToPosition(position);
            }
        });
    }

    private MenuItem menuItem;

    @Override
    protected int menu() {
        return R.menu.group_remove_member;
    }

    @Override
    protected void afterMenus(Menu menu) {
        menuItem = menu.findItem(R.id.remove);
        super.afterMenus(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.remove) {
            removeMember();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void removeMember() {
        List<GroupMemberInfoUI> groupMemberInfoUIS = pickerUserAdapter.getmList();
        ArrayList<String> list = new ArrayList<>();
        for (GroupMemberInfoUI groupMemberInfoUI : groupMemberInfoUIS) {
            list.add(groupMemberInfoUI.memberId);
        }
        IMSipPhoneCtrl.getInstance().deleteGroupMember(groupId, list, imResponse -> {
            String result = imResponse.getCode() == 0 ? "删除成员成功" : imResponse.getMsg();
            runOnUiThread(() -> {
                Toast.makeText(RemoveMemberActivity.this, result, Toast.LENGTH_SHORT).show();
                if (imResponse.getCode() == 0) {
                    finish();
                }
            });
        });
    }
}
